//=======================================================================================

var JPos = {};
(function($){
	
	$.getAbsPos = function(pTarget){
		var x_ = y_ = 0;
		
		if(pTarget.style.position != "absolute"){		
			while(pTarget.offsetParent){
					x_ += pTarget.offsetLeft;
					y_ += pTarget.offsetTop;
					pTarget = pTarget.offsetParent;
			}
		}
			x_ += pTarget.offsetLeft;
			y_ += pTarget.offsetTop;
		return {x:x_,y:y_};
	}
	
	$.getEventPos = function(evt){
			var _x,_y;
			evt = JEvent.getEvent(evt);
			if(evt.pageX || evt.pageY){
				_x = evt.pageX;
				_y = evt.pageY;
			}else if(evt.clientX || evt.clientY){
				_x = evt.clientX + (document.body.scrollLeft || document.documentElement.scrollLeft) - (document.body.clientLeft || document.documentElement.clientLeft);
				_y = evt.clientY + (document.body.scrollTop || document.documentElement.scrollTop) - (document.body.clientTop || document.documentElement.clientTop);
			}else{
				return $.getAbsPos(evt.target);	
			}
			return {x:_x,y:_y};	
	}	
	
})(JPos)

//=======================================================================================

var JEvent = {};
(function($){
	$.getEvent = function(evt){			  
		evt = window.event || evt;
					
		if(!evt){
			var fun = JEvent.getEvent.caller;
			while(fun != null){
				evt = fun.arguments[0];
				if(evt && evt.constructor == Event)
					break;
				fun = fun.caller;
			}
		}
			
		return evt;
	}
	
	$.doEvent = function(fun){
		var args = arguments;
		return function(){
			return fun(args);
		}
	}
})(JEvent)

//=======================================================================================

/*
注意的事项：
Opera,Safari下，z-index = 10000000000 无效,最大能设置的z-index为:999999999

模拟拖动时，最大的问题是当当前有内容被选中，这时的模拟就会变成真的拖动，所以，要把选中的内容取消。
*/

//=======================================================================================

var JDrag = {};
(function($){
	var mAWidth = 200;
	var mAHeight = 200;
	
	var lastObj = null;
	
	var $c = function(p){
		return document.createElement(p);
	}
	
	var $$ = function(p){
		return typeof(p) == "object" ? p : document.getElementById(p);
	}
	
	var mArea = $c("DIV");
	
	if(!document.body){
		document.write("<body></body>");
	}
	
	document.body.appendChild(mArea); 
	//有颜色和无颜色的运行表现是不一样的！为了看见鼠标下面的东东，设置透明度为0
	mArea.style.cssText = "width:" + mAWidth + "px;height:" + mAHeight + "px;position:absolute;background-color:#000;filter: Alpha(Opacity=0);-moz-opacity:0;opacity: 0;display:none;z-index:999999999;display:none;";		
	
	var x,y;
	
	$.setDrag = function(obj){
		obj.style.position = "absolute";

		var dragArea = $$(arguments[1]) || obj;
		
		if(arguments[2] !== false){
			dragArea.onmousedown = JEvent.doEvent(dragArea_mousedown,obj,dragArea);
			obj.onmousedown = JEvent.doEvent(changeZIndex,obj);
		}else{
			dragArea.onmousedown = "";
			obj.onmousedown = "";
		}
	}

	var changeZIndex = function(args){
		if(lastObj){
			$$(lastObj).style.zIndex = 999999997;
			$$(args[1]).style.zIndex = 999999998;
		}
		lastObj = args[1];		
	}
	
	var dragArea_mousedown = function(args){
		try{
			if(document.selection){//IE ,Opera
				if(document.selection.empty)
					document.selection.empty();//IE
				else{//Opera
					document.selection = null;
				}
			}else if(window.getSelection){//FF,Safari
				window.getSelection().removeAllRanges();
			}
		}catch(e){}
		
		var evt = JEvent.getEvent();
		var mPos = JPos.getEventPos(evt);
		
		x = mPos.x;
		y = mPos.y;
		
		with(mArea.style){
			width = mAWidth + "px";
			height = mAHeight + "px";
			left = mPos.x - mAWidth / 2 + "px";
			top = mPos.y - mAHeight / 2 + "px";
			cursor = "move";
			display = "";
		}
			
		mArea.onmousemove = JEvent.doEvent(mArea_mousemove,args[1],args[2]);
		mArea.onmouseout = mArea.onmouseup = JEvent.doEvent(mArea_mouseup,args[1],args[2]);
	}	
	
	var mArea_mouseup = function(args){		
		with(mArea.style){
			width = "1px";
			height = "1px";
			display = "none";
		}
	}
	
	var mArea_mousemove = function(args){
		var evt = JEvent.getEvent();
		var oPos = JPos.getAbsPos(args[1]);
		var mPos = JPos.getEventPos(evt);
		
		mArea.style.left = mPos.x - mAWidth / 2 + "px";
		mArea.style.top = mPos.y - mAHeight / 2 + "px";
		
		args[1].style.left = oPos.x + mPos.x - x + "px";
		args[1].style.top = oPos.y + mPos.y - y + "px";
		
		x = mPos.x;
		y = mPos.y;
	}
	
})(JDrag)

//=======================================================================================

function JWin(pWidth,pHeight,pParent){
	$ = this;
	
	var tpl = '<TABLE style="BORDER-COLLAPSE: collapse" cellSpacing="0" cellPadding="0" width="100%" border="0"><TBODY><TR><TD class="title_left_top" style="CURSOR: nw-resize"></TD><TD class="title_center_top" style="CURSOR: n-resize"></TD><TD class="title_right_top" style="CURSOR: ne-resize"></TD></TR><TR><TD class="title_left_middle" style="CURSOR: nw-resize"></TD><TD class="title_center_middle"><table width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td class="sysIconArea"></td><td class="winTitleArea"></td><td class="sysBtnArea"></td></tr></table></TD><TD class="title_right_middle" style="CURSOR: ne-resize"></TD></TR><TR><TD class="title_left_bottom"></TD><TD class="title_center_bottom"></TD><TD class="title_right_bottom"></TD></TR><TR><TD class="mainBody_left" style="CURSOR: w-resize"></TD><TD class="mainBody_center"><div></div></TD><TD class="mainBody_right" style="CURSOR: e-resize"></TD></TR><TR><TD class="status_left_top"></TD><TD class="status_center_top"></TD><TD class="status_right_top"></TD></TR><TR><TD class="status_left_middle" style="CURSOR: sw-resize"></TD><TD class="status_center_middle"></TD><TD class="status_right_middle" style="CURSOR: se-resize"></TD></TR><TR><TD class="status_left_bottom" style="CURSOR: sw-resize"></TD><TD class="status_center_bottom" style="CURSOR: s-resize"></TD><TD class="status_right_bottom" style="CURSOR: se-resize"></TD></TR></TBODY></TABLE>';
	
	var $c = function(p){
		return document.createElement(p);
	}
	
	var $ = function(p){
		return typeof(p) == "object" ? p : document.getElementById(p);
	}	
	
	var body = $(pParent) || document.body;
	
	var width = pWidth;
	var height = pHeight;
	var skin = "";
	
	var resizeAble = true;
	
	var objs = {
			oOutline:null,
			oWinTitle:null,
			oSysBtnArea:null,
			oMainBody:null,
			oStatus:null
		};
	
	var sysBtns = {
			oMinBtn:null,
			oMaxBtn:null,
			oCloseBtn:null
		};
	
	var vars = {
			original:{}
		};
	
	this.setSkin = function(pSkin){
		skin = pSkin;
		if(objs.oOutline){
			objs.oOutline.className = skin;
		}
		calcSize();
	}
	
	this.create = function(){
		objs.oOutline = $c("DIV");
		body.appendChild(objs.oOutline);
		objs.oOutline.className = skin;
		objs.oOutline.style.width = width + "px";
		objs.oOutline.style.height = height + "px";
		objs.oOutline.style.position = "relative";
		
		objs.oOutline.innerHTML = tpl;
		vars.table = objs.oOutline.firstChild;
		
		var tab = objs.oOutline.getElementsByTagName("TABLE")[1];
		
		objs.oWinTitle = tab.rows[0].cells[1];
		objs.oSysBtnArea = tab.rows[0].cells[2];
		objs.oMainBody = vars.table.rows[3].cells[1].firstChild;
		objs.oMainBody.style.overflow = "auto";
		objs.oStatus = vars.table.rows[5].cells[1];
		
		
		vars.otherHeight = vars.table.rows[0].clientHeight
						 + vars.table.rows[2].clientHeight
						 + vars.table.rows[4].clientHeight
						 + vars.table.rows[6].clientHeight;	
		calcSize();
	}
	
	var calcSize = function(){
		var w = arguments[0];
		var h = arguments[1] || height;
		
			vars.otherHeight = vars.table.rows[0].clientHeight
							 + vars.table.rows[2].clientHeight
							 + vars.table.rows[4].clientHeight
							 + vars.table.rows[6].clientHeight;			
		
		//Safari下,这句怎么不起作用?
		vars.table.rows[3].style.height = h - vars.otherHeight - vars.table.rows[1].clientHeight - vars.table.rows[5].clientHeight + "px";
		objs.oMainBody.style.height = vars.table.rows[3].style.height;
		//window.status = h +" "+ vars.otherHeight +" "+ vars.table.rows[1].clientHeight +" "+ vars.table.rows[2].clientHeight;
	}
	
	this.getTitle = function(){
		return objs.oWinTitle;	
	}
	
	this.getBody = function(){
		return objs.oMainBody;	
	}
	
	this.getStatus = function(){
		return objs.oStatus;	
	}
	
	this.setTitle = function(p){
		typeof(p) == "object" ? objs.oWinTitle.appendChild($(p)) : objs.oWinTitle.innerHTML = p;
		calcSize();
	}
	
	this.setBody = function(p){
		typeof(p) == "object" ? objs.oMainBody.appendChild($(p)) : objs.oMainBody.innerHTML = p;
		calcSize();
	}
	
	this.setStatus = function(p){
		typeof(p) == "object" ? objs.oStatus.appendChild($(p)) : objs.oStatus.innerHTML = p;
		calcSize();
	}
	
	this.setDrag = function(f){
		JDrag.setDrag(objs.oOutline,objs.oWinTitle,f);
	}
	
	var sysBtn_mouseover = function(){
		this.className = this.className.replace("normal","active");
	}
	
	var sysBtn_mouseout = function(){
		this.className = this.className.replace("active","normal");
	}
	
	this.setSysButtons = function(btnMin,btnMax,btnClose){
		var w = 0;
		if(btnMin !== false){			
			sysBtns.oMinBtn = $c("DIV");
			objs.oSysBtnArea.appendChild(sysBtns.oMinBtn);
			sysBtns.oMinBtn.className = "minBtn_normal";
			
			sysBtns.oMinBtn.onmouseover = sysBtn_mouseover;
			sysBtns.oMinBtn.onmouseout = sysBtn_mouseout;
			
			w += sysBtns.oMinBtn.clientWidth;
		}
		
		if(btnMax !== false){				
			sysBtns.oMaxBtn = $c("DIV");
			objs.oSysBtnArea.appendChild(sysBtns.oMaxBtn);
			sysBtns.oMaxBtn.className = "maxBtn_normal";				
				
			sysBtns.oMaxBtn.onmouseover = sysBtn_mouseover;
			sysBtns.oMaxBtn.onmouseout = sysBtn_mouseout;				
			//sysBtns.oMaxBtn.onclick = $.maxSize;
			sysBtns.oMaxBtn.onclick = maxSize;
			
			w += sysBtns.oMaxBtn.clientWidth;
		}
				
		if(btnClose !== false){				
			sysBtns.oCloseBtn = $c("DIV");
			objs.oSysBtnArea.appendChild(sysBtns.oCloseBtn);
			sysBtns.oCloseBtn.className = "closeBtn_normal";
				
			sysBtns.oCloseBtn.onmouseover = sysBtn_mouseover;
			sysBtns.oCloseBtn.onmouseout = sysBtn_mouseout;				
			sysBtns.oCloseBtn.onclick = this.hidden;
				
			w += sysBtns.oCloseBtn.clientWidth;
		}
		
		objs.oSysBtnArea.style.width = w + "px";
	}
	
	/*------------------------------------------------------
	我很疑惑:把maxSize,restoreSize改成public的,sysBtns就读不到该实例的了(如果有多个实例),研究........
	-------------------------------------------------------*/
	
	var maxSize = function(){
		
		vars.original.width		= objs.oOutline.clientWidth + "px";
		vars.original.height	= objs.oOutline.clientHeight + "px";
		vars.original.top		= objs.oOutline.style.top;
		vars.original.left		= objs.oOutline.style.left;
		
		var w = document.documentElement.clientWidth;
		var h = document.documentElement.clientHeight;
		
		document.body.appendChild(objs.oOutline);
		with(objs.oOutline.style){
			left = "0px";
			top = "0px";
			position = "absolute";
			width = w + "px";
			height = h + "px";
		}
		
		calcSize(w,h);
		
		sysBtns.oMaxBtn.className = "restoreBtn_normal";
		sysBtns.oMaxBtn.onclick = restoreSize;
	}
	
	var restoreSize = function(){
		try{
			
			body.appendChild(objs.oOutline);
			with(objs.oOutline.style){
				width = vars.original.width;
				height = vars.original.height;
				top = vars.original.top;
				left = vars.original.left;
			}
			
			calcSize();
			sysBtns.oMaxBtn.className = "maxBtn_normal";
			sysBtns.oMaxBtn.onclick = maxSize;
		}catch(e){
			//alert(e.message)	
		}
	}
	
	this.moveTo = function(x,y){		
		with(objs.oOutline.style){
			position = "absolute";
			left = x + "px";
			top = y + "px";
		}
	}
	
	this.show = function(){		
		objs.oOutline.style.display = "";
	}
	
	this.hidden = function(){		
		objs.oOutline.style.display = "none";
		restoreSize();
	}
}